﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Http {
    /// <summary>
    /// 拓展类
    /// </summary>
    public static partial class Extention {
        public static string MapPath(this HttpContext httpContext,string virtualPath) {
            IActionContextAccessor actionContext = new ActionContextAccessor();
            UrlHelper urlHelper = new UrlHelper(actionContext.ActionContext);
            virtualPath = urlHelper.Content(virtualPath);

            return $"{Path.Combine(new List<string> { actionContext.ActionContext.HttpContext.MapPath("") }.Concat(virtualPath.Split('/')).ToArray())}";
        }
        public static string GetUrlReferrer(this HttpContext httpContext) {

            //URL referrer is null in some case (for example, in IE 8)
            return httpContext.Request.Headers[HeaderNames.Referer];
        }
    }
}
